<?php

namespace common\entities\myhtsq;

use common\entities\ContractEntity;
use Yii;

/**
 * This is the model class for table "myscrm_contract_manage".
 *
 * @property string $contract_id
 * @property integer $customer_property
 * @property string $customer_id
 * @property string $customer_code
 * @property string $customer_name
 * @property string $top_customer_name
 * @property string $customer_full_name
 * @property string $contract_property_id
 * @property string $company_id
 * @property string $area_id
 * @property string $contract_code
 * @property string $category_id
 * @property string $big_category_id
 * @property string $littler_category_id
 * @property string $sign_date
 * @property string $input_date
 * @property string $sign_people_id
 * @property string $sign_amount
 * @property string $rent_amount
 * @property string $delivery_amount
 * @property string $application_amount
 * @property string $application_deal_amount
 * @property string $application_deal_discount
 * @property string $contract_deal_discount
 * @property integer $draw_project_numeric
 * @property integer $balance_type
 * @property string $balance_rate
 * @property string $balance_amount
 * @property integer $balance_status
 * @property string $back_amount
 * @property string $balance_remark
 * @property boolean $is_violated
 * @property string $account_company_id
 * @property integer $contract_status
 * @property string $reviewer_id
 * @property integer $is_cancelled
 * @property string $review_date
 * @property string $created_on
 * @property string $created_by
 * @property string $modified_on
 * @property string $modified_by
 * @property string $balance_date
 * @property string $give_work_day
 * @property string $develop_work_day
 * @property string $implement_work_day
 * @property string $sign_number
 * @property string $erp_contract_code
 * @property string $order_code
 * @property string $sign_id
 */
class ContractManageEntity extends ContractEntity
{
	/**
	 * 已审核
	 * @var int
	 */
	const APPROVE_STATUS_PASS = 2;
	/**
	 * 提交复核
	 * @var int
	 */
	const APPROVE_STATUS_SUBMIT_HANDLE = 1;
	
	/**
	 * 草稿
	 * @var int
	 */
	const APPROVE_STATUS_DRAFT = 0;
	
	/**
	 * 作废合同
	 * @var int
	 */
	const CONTRACT_IS_CANCELLED = 1;
	
	/**
	 * 合同不作废
	 */
	const CONTRACT_NO_CANCELLED = 0;
	
	/**
	 * 合同已结算
	 * @var int
	 */
	const CONTRACT_BALANCE_STATUS = 1;
	/**
	 * 合同未结算
	 * @var int
	 */
	const CONTRACT_NOT_BALANCE_STATUS = 0;
	
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'myscrm_contract_manage';
    }

    public static function globalTableName()
    {
        preg_match("/dbname=([^;]+)/i", self::getDb()->dsn, $matches);
        return $matches[1].'.myscrm_contract_manage';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['contract_id'], 'safe'],
            [['customer_property', 'draw_project_numeric', 'balance_type', 'balance_status', 'contract_status', 'is_cancelled', 'project_numeric','repayment_type'], 'integer'],
            [['sign_date', 'input_date', 'review_date', 'created_on', 'modified_on','balance_date','contract_id','effect_date'], 'safe'],
            [['is_violated','sign_amount', 'rent_amount', 'delivery_amount', 'application_amount', 'application_deal_amount', 'application_deal_discount', 'contract_deal_discount', 'balance_rate', 'balance_amount', 'back_amount','develop_work_day','give_work_day','implement_work_day', 'income_amount'], 'number'],
            [['balance_remark'], 'string'],
            [['contract_id', 'erp_contract_id', 'customer_id', 'contract_property_id', 'company_id', 'area_id', 'category_id', 'big_category_id', 'littler_category_id', 'sign_people_id', 'account_company_id', 'reviewer_id', 'created_by', 'modified_by','sign_people_department_by','repayment_company', 'sign_id'], 'string', 'max' => 36],
            [['customer_name', 'top_customer_name', 'customer_code', 'erp_contract_code', 'order_code'], 'string', 'max' => 50],
            [['attribute_name', 'sign_number'], 'string', 'max' => 50],
            [['customer_full_name', 'sign_people_abu_name', 'contract_code'], 'string', 'max' => 100],
            [['contract_remark', 'achievement_remark'], 'string', 'max' => 500]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'contract_id' => '合同ID',
            'customer_property' => '客户性质0:开发商 1:代理商',
            'customer_id' => '客户ID',
            'customer_code' => '客户code',
            'customer_name' => '客户名称',
            'top_customer_name' => '顶级客户简称',
            'customer_full_name' => '签约客户全称',
            'contract_property_id' => '合同性质ID',
            'company_id' => '合同签署公司ID',
            'area_id' => '合同签署区域ID',
            'contract_code' => '合同编号',
            'category_id' => '合同类别ID',
            'big_category_id' => '合同大类ID',
            'littler_category_id' => '合同小类ID',
            'sign_date' => '合同签署日期',
            'input_date' => '合同录入日期',
            'sign_people_id' => '签约人ID',
            'sign_amount' => '签约金额',
            'rent_amount' => '租用金额',
            'delivery_amount' => '交付金额',
            'application_amount' => '合同产品标准总额',
            'application_deal_amount' => '合同产品成交总额',
            'application_deal_discount' => '产品成交折扣',
            'contract_deal_discount' => '合同成交折扣',
            'draw_project_numeric' => '提成产品数',
            'balance_type' => '结算类型 0：标准结算 1：特价结算 2：特批其他',
            'balance_rate' => '结算比例',
        	'balance_date' => '结算时间',	
            'balance_amount' => '结算金额',
            'balance_status' => '结算状态 0:未结算，1已结算',
            'back_amount' => '返款金额',
            'balance_remark' => '结算备注',
            'is_violated' => '是否违规合同 0：否 1：是',
            'account_company_id' => '套账公司',
            'contract_status' => '合同状态',
            'reviewer_id' => '复核人ID',
            'is_cancelled' => '是否作废：0=否，1=是',
            'review_date' => '复核时间',
            'created_on' => '创建时间',
            'created_by' => '创建人',
            'modified_on' => '修改时间',
            'modified_by' => '修改人',
            'give_work_day' => '赠送工作量',
            'develop_work_day' => '开发工作量',
            'implement_work_day' => '实施工作量',
            'project_numeric' => '项目数',
            'contract_remark' => '合同备注',
            'achievement_remark' => '业绩备注',
            'attribute_name' => '属性名称',
            'sign_people_department_by' => '签约人所属部门',
            'sign_people_abu_name' => '签约人所属abu名称',
            'repayment_time' => '返款时间',
            'repayment_type' => '返款类型:1现金,2转预收',
            'repayment_company' => '服务机构',
            'sign_number' => '会签单号',
			'order_code'  => '出库单据号',
            'sign_id' => '会签id',
        ];
    }


    /**
     * 获取合同基础信息
     *
     * @param  $map        筛选条件
     * @return array|false
     */
    public function getContractInfo($map = []){
        if(empty($map)){
            return false;
        }

        //sonar 坏味道修复
        return self::find()->select('*')->where($map)->one();
    }
}
